home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-03-12 | 7.7 KB | 327 lines | [TEXT/CWIE] |
- /*
- ********************************************************************************
- **
- ** Name: GSpTest_DSpSupport.cp
- **
- ** Description:
- **
- ** Support routines for using DrawSprocket.
- **
- ********************************************************************************
- */
- #include "DrawSprocket.h"
-
- #include "GSpTest_DSpSupport.h"
- #include "GSpTest_Main.h"
-
- /*
- ********************************************************************************
- ** constants
- ********************************************************************************
- */
- #define kDisplayWidth 640
- #define kDisplayHeight 480
- #define kDisplayDepth 8
- #define kDisplayPages 2
-
- /*
- ********************************************************************************
- ** globals
- ********************************************************************************
- */
- DSpContextAttributes gDSpContextAttributes;
- DSpContextReference gDSpContext;
-
- /*
- ********************************************************************************
- ** private globals
- ********************************************************************************
- */
- static Boolean gDSpStarted = false;
-
- /*
- ********************************************************************************
- ** local prototypes
- ********************************************************************************
- */
- static void MyInitContextAttributes( DSpContextAttributes *inAttributes );
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayInit()
- **
- ** Description:
- **
- ** Find the best context to use and reserve it.
- **
- ********************************************************************************
- */
- OSStatus // result code
- DisplayInit( void )
- {
- OSStatus theError;
-
- // startup DrawSprocket
- theError = DSpStartup();
- if( theError )
- return theError;
- gDSpStarted = true;
-
- // find the best context
- MyInitContextAttributes( &gDSpContextAttributes );
- theError = DSpFindBestContext( &gDSpContextAttributes, &gDSpContext );
- if( theError )
- return theError;
-
- // reserve the context
- theError = DSpContext_Reserve( gDSpContext, &gDSpContextAttributes );
- if( theError )
- {
- gDSpContext = NULL;
- return theError;
- }
-
- return noErr;
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayActivate()
- **
- ** Description:
- **
- ** Activate the display.
- **
- ********************************************************************************
- */
- OSStatus // result code
- DisplayActivate( void )
- {
- // make sure we have a context
- if( NULL == gDSpContext )
- return noErr;
-
- return DSpContext_SetState( gDSpContext, kDSpContextState_Active );
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayPause()
- **
- ** Description:
- **
- ** Pause the display.
- **
- ********************************************************************************
- */
- OSStatus // result code
- DisplayPause( void )
- {
- DSpContextState theState;
- OSStatus theError;
-
- // make sure we have a context
- if( NULL == gDSpContext )
- return noErr;
-
- // a context must be active before it can be paused
- theError = DSpContext_GetState( gDSpContext, &theState );
- if( theError )
- return theError;
- if( theState != kDSpContextState_Active )
- {
- theError = DisplayActivate();
- if( theError )
- return theError;
- }
-
- return DSpContext_SetState( gDSpContext, kDSpContextState_Paused );
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayFadeOut()
- **
- ** Description:
- **
- ** Fade the display out.
- **
- ********************************************************************************
- */
- OSStatus // result code
- DisplayFadeOut( void )
- {
- // make sure we have a context
- if( NULL == gDSpContext )
- return noErr;
-
- return DSpContext_FadeGammaOut( NULL, NULL );
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayFadeIn()
- **
- ** Description:
- **
- ** Fade the display in.
- **
- ********************************************************************************
- */
- OSStatus // result code
- DisplayFadeIn( void )
- {
- // make sure we have a context
- if( NULL == gDSpContext )
- return noErr;
-
- return DSpContext_FadeGammaIn( NULL, NULL );
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayRelease
- **
- ** Description:
- **
- ** Release the display.
- **
- ********************************************************************************
- */
- OSStatus // result code
- DisplayRelease( void )
- {
- OSStatus theError;
-
- // make sure we have a context
- if( NULL == gDSpContext )
- return noErr;
-
- // fade out the display
- DisplayFadeOut();
-
- // inactivate it
- DSpContext_SetState( gDSpContext, kDSpContextState_Inactive );
-
- // fade in the display
- DisplayFadeIn();
-
- // release it
- theError = DSpContext_Release( gDSpContext );
- gDSpContext = NULL;
-
- // shutdown DrawSprocket
- if( gDSpStarted )
- {
- DSpShutdown();
- gDSpStarted = false;
- }
-
- return theError;
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayGetBuffer
- **
- ** Description:
- **
- ** Get the current drawing buffer for the display.
- **
- ********************************************************************************
- */
- CGrafPtr
- DisplayGetBuffer( void )
- {
- OSStatus theError;
- CGrafPtr theBuffer = NULL;
-
- // get the buffer
- theError = DSpContext_GetBackBuffer( gDSpContext, kDSpBufferKind_Normal,
- &theBuffer );
- if( theError )
- HandleError( theError, true );
-
- return theBuffer;
- }
-
- /*
- ********************************************************************************
- **
- ** Name: DisplayShowBuffer
- **
- ** Description:
- **
- ** Show the current drawing buffer for the display.
- **
- ********************************************************************************
- */
- OSStatus
- DisplayShowBuffer( void )
- {
- return DSpContext_SwapBuffers( gDSpContext, NULL, NULL );
- }
-
- /******************************************************************************/
- #pragma mark -
- #pragma mark ## static functions ##
-
- /*
- ********************************************************************************
- **
- ** Name: MyInitContextAttributes
- **
- ** Description:
- **
- ** Initialize a context attributes structure so that there is no garbage
- ** data in it. This is important to do because DS will return an error
- ** if some fields are set incorrectly (including the "reserved" fields not
- ** being set to zero), and some things can actually cause a crash (such
- ** as a bogus color table handle).
- **
- ********************************************************************************
- */
- static void
- MyInitContextAttributes(
- DSpContextAttributes *inAttributes /* attr structure to init */
- )
- {
- // init the context attributes structure
- inAttributes->frequency = 0;
- inAttributes->displayWidth = 0;
- inAttributes->displayHeight = 0;
- inAttributes->reserved1 = 0;
- inAttributes->reserved2 = 0;
- inAttributes->colorNeeds = 0;
- inAttributes->colorTable = NULL;
- inAttributes->contextOptions = 0;
- inAttributes->backBufferDepthMask = 0;
- inAttributes->displayDepthMask = 0;
- inAttributes->backBufferBestDepth = 0;
- inAttributes->displayBestDepth = 0;
- inAttributes->pageCount = 0;
- inAttributes->gameMustConfirmSwitch = false;
- inAttributes->reserved3[0] = 0;
- inAttributes->reserved3[1] = 0;
- inAttributes->reserved3[2] = 0;
- inAttributes->reserved3[3] = 0;
-
- // override the values that I want to customize
- inAttributes->displayWidth = kDisplayWidth;
- inAttributes->displayHeight = kDisplayHeight;
- inAttributes->colorNeeds = kDSpColorNeeds_Require;
- inAttributes->colorTable = NULL;
- inAttributes->backBufferDepthMask = kDisplayDepth;
- inAttributes->backBufferBestDepth = kDisplayDepth;
- inAttributes->displayDepthMask = kDisplayDepth;
- inAttributes->displayBestDepth = kDisplayDepth;
- inAttributes->pageCount = kDisplayPages;
- }
-